package room_event

import (
	"encoding/json"
	"longmen/server/config/global"
	"longmen/server/internal/rpc/tencentim/chat"
	"longmen/server/internal/rpc/tencentim/imsdk"
	"longmen/server/internal/rpc/tencentim/messages"
	"longmen/server/internal/rpc/tencentim/utils"
	pb "longmen/server/pkg/protobuf"

	"github.com/spf13/cast"
)

/*
用户触发风控被禁言
*/
func handlingBanSelfMessage(user *chat.ClientUser, b []byte) (ret *pb.CallbackResp, err error) {
	//fName := "BanSelf"
	oMsg := &messages.BanSelfMsg{}
	err = json.Unmarshal(b, oMsg)
	if err != nil {
		return utils.CallbackFailResp(nil, err.Error()), nil
	}
	iRetry := 3
	for {

		err = imsdk.GetSdk().GlobalForbidMsg(
			cast.ToString(user.UserId),
			true,
			oMsg.TBan)
		if err == nil {
			break //ok了
		}

		iRetry--
		if iRetry <= 0 {
			global.Log.Infof("groupBanError:%s", err.Error())
			return utils.CallbackFailResp(nil, err.Error()), nil
		}
	}
	AddBanRecord(user.UserId, 0, 0, 9, oMsg.TBan, "发言触发风控", oMsg.BanMsg)
	ret = &pb.CallbackResp{}
	ret.ErrorCode = 0
	return utils.CallbackOkResp(nil), nil
}
